﻿using System;
using CardLogistics.Model.Purchase;
using CardLogistics.Web.Components;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Data;
using System.Collections.Generic;
using CardLogistics.DAL;

namespace CardLogistics.Web.MainPage.Purchase.Demand
{
    public partial class AddEdit : BasePage
    {
        private static readonly CardLogistics.DAL.Purchase.Purchase_DemandData DAL = new CardLogistics.DAL.Purchase.Purchase_DemandData();
        private static readonly CardLogistics.DAL.Purchase.Purchase_DetailedData DDAL = new CardLogistics.DAL.Purchase.Purchase_DetailedData();
        string demandId = "";//采购需求编号Id
        protected Dictionary<string, string> CardTypeList = null;
        public string TitleName
        {
            get;
            set;
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            CardTypeList = GetCardTypeList();
            if (!IsPostBack)
            {
                DEMANDDEPARTMENT.Value = "运行管理部";//默认运行管理部
                InitControl();
            }
        }

        #region 获取卡类型
        private Dictionary<string, string> GetCardTypeList()
        {
            // var BranchArray = CardTypeList.ToDictionary(k => k.ID, k => k.Name);可以把数据库查出来的list转换成Dictionary
            Dictionary<string, string> cardTypeList = new Dictionary<string, string>();
            DataTable dt = CardLogistics.DAL.DbHelperSQL.GetQueryResult("select id,cardtypename from basic_cardtype where STATUS=0 and ISDELETE=0 and FathercardtypeId !='0'");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                string id = dt.Rows[i]["id"].ToString();
                string cardtypename = dt.Rows[i]["cardtypename"].ToString();
                cardTypeList.Add(id, cardtypename);
            }
            return cardTypeList;
        }
        #endregion

        #region 编辑时绑定值
        /// <summary>
        /// 编辑时绑定值
        /// </summary>
        public void InitControl()
        {
            TitleName = GetStatusInfo(0, "采购需求");
            if (IsEdit)
            {
                string _key = Request.QueryString["id"];
                if (!string.IsNullOrEmpty(_key))
                {
                    Purchase_Demand pData = DAL.GetModel(_key);
                    SetWebControls(this.Page, pData);
                    DataTable dt = DDAL.GetInfoByDemandId(pData.ID);
                    string detId = "";
                    if (dt != null && dt.Rows.Count > 0)
                    {
                        foreach (DataRow dr in dt.Rows)
                        {
                            detId += dr["CARDTYPE"].ToString() + "|" + dr["CARDNUMBER"].ToString() + ";";
                        }
                        hf_ConId.Value = detId.TrimEnd(';');
                    }
                    btn_Add.Visible = btn_Submit.Visible = false;
                    if (pData.IsSubmit == 0 || pData.IsSubmit == 3)//状态为 0(未提交) 或 3(驳回)时才可以进行编辑操作
                    {
                        btn_Add.Visible = btn_Submit.Visible = true;
                    }
                    TitleName = GetStatusInfo(pData.IsSubmit, "采购需求");
                }
            }
            else
            {
                BILLNO.Value = GetBillNumber("采购需求");
            }

        }
        #endregion

        #region 保存
        /// <summary>
        /// 保存按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_Add_Click(object sender, EventArgs e)
        {
            try
            {
                string result = EncapsulationModel(0);
                ShowMegByResult(result, 0);
            }
            catch (Exception ex)
            {
                ShowMessageBox("系统异常:" + ex.ToString());
            }
        }
        #endregion

        #region 保存并提交
        /// <summary>
        /// 保存并提交
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_Submit_Click(object sender, EventArgs e)
        {
            try
            {
                string result = EncapsulationModel(1);
                ShowMegByResult(result, 1);
            }
            catch (Exception ex)
            {
                ShowMessageBox("系统异常:" + ex.ToString());
            }
        }
        #endregion

        #region 提示信息
        /// <summary>
        /// 提示信息
        /// </summary>
        /// <param name="result"></param>
        private void ShowMegByResult(string result, int issubmit)
        {
            if (result.Split(',')[0] == "ok")
            {
                if (issubmit == 1)
                {
                    InsertAppInfo(0, result.Split(',')[1]);
                    InsertLog("保存并提交需求信息,需求单号:" + BILLNO.Value);
                }
                else
                    InsertLog("保存需求信息,需求单号:" + BILLNO.Value);
                ReturnListInfo();
            }
            else
            {
                ShowMessageBox("操作失败请重新操作！" + result.Split(',')[1]);
            }
        }
        #endregion

        #region 得到对象
        /// <summary>
        /// 需求信息
        /// </summary>
        /// <param name="issubmit">是否提交</param>
        /// <returns></returns>
        private string EncapsulationModel(int issubmit)
        {
            string result = string.Empty;
            Purchase_Demand model = new Purchase_Demand();
            model = GetWebControls<Purchase_Demand>(this.Page);
            model.LASTUPDATEUSER = LoggedUser.ID;
            model.LASTUPDATEDATE = DateTime.Now;
            model.ISDELETE = 0;
            model.IsSubmit = issubmit;
            model.Details = hf_ConId.Value + ";";
            demandId = model.ID;//获取采购需要编号ID
            if (IsEdit)
            {
                model.ID = Request.QueryString["id"];
                result = DAL.Update(model);
            }
            else
            {
                result = DAL.Add(model);
                if (result.Split(',')[0] == "ok")
                {
                    ChgBillNumber("采购需求");
                }
            }
            return result;
        }
        #endregion

    }
}